package com.youlu.campus.service.english;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.base.exception.EnglishMobileReceiveException;
import com.youlu.campus.component.SmsUtil;
import com.youlu.campus.entity.VO.req.CheckMobileVO;
import com.youlu.campus.entity.VO.res.EnglishMobileReceiveRes;
import com.youlu.campus.entity.dto.sms.SmsBaseInfoDTO;
import com.youlu.campus.entity.english.CompetitionConfig;
import com.youlu.campus.entity.english.UserCompetition;
import com.youlu.campus.service.sms.SmsServerService;
import com.youlu.campus.service.visitor.VisitorUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class MobileReceiveService {

    @Autowired
    private UserCompetitionService userCompetitionService;

    @Autowired
    private CompetitionConfigService competitionConfigService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SmsUtil smsUtil;

    @Autowired
    private SmsServerService smsServerService;

    private final static String MOBILE_CODE = "campus:english:mobile:%s";


    @Autowired
    private VisitorUserService visitorUserService;

    public EnglishMobileReceiveRes login(CheckMobileVO req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        if (StringUtils.isBlank(mobile)) {
            throw new BusinessException("对不起，请填写手机号哦!");
        }
        log.info("用户输入手机号 {}验证领取电子证书", mobile);
        EnglishMobileReceiveRes englishMobileReceive = new EnglishMobileReceiveRes();
        String mobileFormat = String.format(MOBILE_CODE, mobile);
        String formatMobileCode = redisTemplate.opsForValue().get(mobileFormat);
        if (StringUtils.isBlank(formatMobileCode)) {
            if (StringUtils.isBlank(code)) {
                throw new BusinessException("对不起,验证码不能为空");
            }
            SmsBaseInfoDTO dto = SmsBaseInfoDTO.builder().phone(req.getMobile()).code(req.getCode()).build();
            Pair<Boolean, String> result = smsServerService.checkVerifyCode(dto);
            if (!result.getLeft()) {
                throw new BusinessException(result.getRight());
            }
            this.redisTemplate.opsForValue().set(mobileFormat, mobile, 1L, TimeUnit.DAYS);
        }
        englishMobileReceive.setAuthorized(true);
        englishMobileReceive.setBindMobile(true);
        UserCompetition userCompetition = userCompetitionService.findByMobile(mobile);
        if (Objects.isNull(userCompetition)) {
            CompetitionConfig competitionConfig = competitionConfigService.detail();
            if (Objects.nonNull(competitionConfig) && StringUtils.isNotBlank(competitionConfig.getReceiveErrorMsg())) {
                throw new EnglishMobileReceiveException(competitionConfig.getReceiveErrorMsg());
            } else {
                throw new BusinessException("对不起,未报名参加该活动");
            }
        }
        englishMobileReceive.setId(userCompetition.getUserId());
        englishMobileReceive.setUniversityId(userCompetition.getUniversityId());
        englishMobileReceive.setUniversityName(userCompetition.getUniversityName());
        englishMobileReceive.setOpenId(userCompetition.getOpenId());
        englishMobileReceive.setNickName(userCompetition.getNickName());
        englishMobileReceive.setAvatarUrl(userCompetition.getAvatarUrl());
        englishMobileReceive.setName(userCompetition.getName());
        return englishMobileReceive;

    }


}
